<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/10
 * Time: 11:58
 */

namespace Timer\Module;


use Timer\Traits\Singleton;
use Timer\Lib\OPT_CONST;

class Tools{
    use Singleton;

    /**
     * 拆解接收的json字符串
     * @param string $jsonString json字符串
     * @return mixed
     */
    public function jsonDecode($jsonString)
    {
        if(empty($jsonString)) {
            return OPT_CONST::JSON_FALSE;
        }
        //判断是否为JSON格式
        if($return = json_decode($jsonString,true)) {
            return $return;
        } else {
            //不是json格式
            return OPT_CONST::JSON_FALSE;
        }
    }

    /**
     * 数组转JSON格式
     * @param $array
     * @return string
     */
    public function jsonEncode($array)
    {
        $this->__arrayRecursive($array, 'urlencode', true);
        $json = json_encode($array);
        return urldecode($json);
    }
    private function __arrayRecursive(&$array, $function, $apply_to_keys_also = false)
    {
        static $recursive_counter = 0;
        if (++$recursive_counter > 1000) {
            Logger::getInstance()->error( "function(checkRequestFormat):possible deep recursion attack"." in file".__FILE__." on Line ".__LINE__);
            return;
        }
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $this->__arrayRecursive($array[$key], $function, $apply_to_keys_also);
            } else if(is_object($value)) {
                $array[$key] = $value;
            } else {
                $array[$key] = $function($value);
            }

            if ($apply_to_keys_also && is_string($key)) {
                $new_key = $function($key);
                if ($new_key != $key) {
                    $array[$new_key] = $array[$key];
                    unset($array[$key]);
                }
            }
        }
        $recursive_counter--;
    }


    /**
     * 判断数据格式是否正确
     * @param $postArr
     * @return array|bool
     */
    public function checkRequestParams($postArr)
    {
        if(!isset($postArr['operation'])) {
            Logger::getInstance()->error( "function(checkRequestParams):lack of operation"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>-20,'operation'=>"",'data'=>array("missing_parameter"=>"operation"),'result_message'=>"缺少参数");
        }

        if(!isset($postArr['room_id'])){
            Logger::getInstance()->error( "function(checkRequestParams):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>-20,'operation'=>$postArr['operation'],'data'=>array("missing_parameter"=>"room_id"),'result_message'=>"缺少参数");
        }
        if(!isset($postArr['data'])) {
            Logger::getInstance()->error( "function(checkRequestParams):lack of data"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>-20,'operation'=>$postArr['operation'],'data'=>array("missing_parameter"=>"data"),'result_message'=>"缺少参数");
        }

        return true;
    }

    /**
     * 检查格式
     * @param $jsonString
     * @return int
     */
    public function checkupRequestFormat($jsonString)
    {
        //解析json字符串为数组
        $jsonArray = $this->jsonDecode($jsonString);
        //判断是否为json格式
        if($jsonArray === OPT_CONST::JSON_FALSE) {
            $result = OPT_CONST::CORRECT_FORMAT;
        } else {
            //判断数据格式是否正确
            $result = $this->checkRequestParams($jsonArray);
            if($result === true) {
                return $jsonArray;
            }
        }
        return $result;
    }

    /**
     * 检查推送格式
     * @param $jsonString
     * @return bool|int
     */
//    public function checkupPostFormat($jsonString)
//    {
//        //解析json字符串为数组
//        $jsonArray = $this->jsonDecode($jsonString);
//        if($jsonArray === OPT_CONST::JSON_FALSE) {  //判断是否为json格式
//            $result = OPT_CONST::CORRECT_FORMAT;
//        } else {
//            $result = OPT_CONST::POSTARRAY_TRUE;
//            //判断数据格式是否正确
//            if(isset($jsonArray['module']) && $jsonArray['module'] == "GW_Push") {
//                if(!isset($jsonArray['type'])) {
//                    $result = OPT_CONST::CORRECT_FORMAT;
//                }
//
//                if(!isset($jsonArray['object_array'])) {
//                    $result = OPT_CONST::CORRECT_FORMAT;
//                }
//
//                if(!isset($jsonArray['content'])) {
//                    $result = OPT_CONST::CORRECT_FORMAT;
//                }
//            } else if(isset($jsonArray['module']) && $jsonArray['module'] == "GW_Server") {
//                if(!isset($jsonArray['operation'])) {
//                    $result = OPT_CONST::CORRECT_FORMAT;
//                }
//            } else {
//                $result = OPT_CONST::CORRECT_FORMAT;
//            }
//
//
//            if($result === OPT_CONST::POSTARRAY_TRUE) {
//                return $jsonArray;
//            }
//        }
//
//        return $result;
//    }

    /**
     * 判断数据格式是否正确
     * @param $requestAry
     * @return array|bool
     */
//    public function checkPostParams($requestAry)
//    {
//        if(!isset($requestAry['msgType'])) {
//            Logger::getInstance()->error( "function(checkRequestParams):lack of msgType"." in file".__FILE__." on Line ".__LINE__);
//            return array('result'=>-20,'operation'=>"",'data'=>array("missing_parameter"=>"msgType"),'result_message'=>"缺少参数");
//        }
//        if(!isset($requestAry['content'])) {
//            Logger::getInstance()->error( "function(checkRequestParams):lack of content"." in file".__FILE__." on Line ".__LINE__);
//            return array('result'=>-20,'operation'=>"",'data'=>array("missing_parameter"=>"content"),'result_message'=>"缺少参数");
//        }
//        return OPT_CONST::POSTARRAY_TRUE;
//    }


}